<?php

namespace addon\hotel\hotel\controller;

use addon\hotel\model\HotelMember as HotelMemberModel;
use app\model\member\MemberAccount as MemberAccountModel;
use app\model\member\MemberAddress;
use app\model\member\MemberAddress as MemberAddressModel;
use app\model\member\Member as MemberModel;

class Member extends BaseHotel
{

    /**
     * 店铺会员列表
     */
    public function lists()
    {
        $member = new HotelMemberModel();
        if (request()->isAjax()) {
            $page_index = input('page', 1);
            $page_size = input('limit', PAGE_LIST_ROWS);
            $search_text = input('search_text', '');
            $search_text_type = input('search_text_type', 'nickname');
            $start_date = input('start_date', '');
            $end_date = input('end_date', '');

            $condition = [
                [ 'nsm.hotel_id', '=', $this->hotel_id ]
            ];
            $condition[] = [ $search_text_type, 'like', "%" . $search_text . "%" ];
            // 关注时间
            if ($start_date != '' && $end_date != '') {
                $condition[] = [ 'nsm.subscribe_time', 'between', [ strtotime($start_date), strtotime($end_date) ] ];
            } else if ($start_date != '' && $end_date == '') {
                $condition[] = [ 'nsm.subscribe_time', '>=', strtotime($start_date) ];
            } else if ($start_date == '' && $end_date != '') {
                $condition[] = [ 'nsm.subscribe_time', '<=', strtotime($end_date) ];
            }
            $list = $member->getHotelMemberPageList($condition, $page_index, $page_size, 'nsm.subscribe_time desc');
            return $list;
        } else {
            return $this->fetch("member/lists", [], $this->replace);
        }
    }


    public function detail()
    {
        $member_model = new MemberModel();

        //会员信息
        $member_id = input('member_id', 0);
        $member_info = $member_model->getMemberInfo([ [ 'member_id', '=', $member_id ] ]);
        $this->assign('member_info', $member_info['data']);
        $parent_nickname = '';
        if ($member_info['data']['source_member']){
            $parent = model('member')->getInfo(['member_id'=>$member_info['data']['source_member']],'nickname');
            $parent_nickname = $parent?$parent['nickname']:'';
        }
        $this->assign('parent_nickname', $parent_nickname);

        $member_address_model = new MemberAddress();

        $member_address_list = $member_address_model->getMemberAddressList([ [ 'member_id', '=', $member_id ] ]);
        $this->assign('member_address_list', $member_address_list['data']);

        return $this->fetch("member/detail", [], $this->replace);
    }


    /**
     * 会员详情
     */
    public function detail1()
    {
        $member_id = input('member_id', 0);


        $member = new HotelMemberModel();
        $condition = [
            [ 'nsm.member_id', '=', $member_id ],
            [ 'nsm.hotel_id', '=', $this->hotel_id ],
            [ 'nm.is_delete', '=', 0 ]
        ];
        $join = [
            [
                'member nm',
                'nsm.member_id = nm.member_id',
                'inner'
            ],
        ];
        $field = 'nm.member_id, nm.source_member, nm.username, nm.nickname, nm.mobile, nm.email, nm.headimg, nm.status, nm.point, nm.balance, nm.balance_money, nm.growth, nsm.subscribe_time, nsm.hotel_id, nsm.is_subscribe,nm.invitation_code';
        $info = $member->getMemberInfo($condition, $field, 'nsm', $join);
        if ($info[ 'code' ] < 0) $this->error($info[ 'message' ]);
        //账户类型和来源类型
        $member_account_model = new MemberAccountModel();
        $account_type_arr = $member_account_model->getAccountType();
        $this->assign('account_type_arr', $account_type_arr);
        $this->assign('info', $info[ 'data' ]);
        return $this->fetch("member/detail", [], $this->replace);
    }

    /**
     * 会员地址
     */
    public function addressDetail()
    {
        if (request()->isAjax()) {
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $member_id = input('member_id', 0);
            $condition[] = [ 'member_id', '=', $member_id ];
            //会员地址
            $member_address_model = new MemberAddressModel();
            $res = $member_address_model->getMemberAddressPageList($condition, $page, $page_size);
            return $res;

        } else {
            $member_id = input('member_id', 0);
            $this->assign('member_id', $member_id);

            //会员详情四级菜单
            $this->forthMenu([ 'member_id' => $member_id ]);

            return $this->fetch('member/address_detail');
        }
    }
}